package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private MemberService memberService;
    private Map map;
    @Autowired
    private OrderService orderService;

    /**
     * 套餐占比（饼状图）
     */
    @GetMapping("/getSetmealReport")
    public Result getReport() {
        Map<String, Object> data = new HashMap<>();
        try {
            List<Map<String, Object>> setmealCount = reportService.getSetmealReport();
            data.put("setmealCount", setmealCount);
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map : setmealCount) {
                //取出套餐名称
                String name = (String) map.get("name");
                setmealNames.add(name);
            }
            data.put("SetmealNames", setmealNames);

            return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, data);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    /**
     * 用户饼状图 yxj
     * @param
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        //使用map接收日期和人数总和
        Map<String,Object> map = new HashMap<>();
        //LIst接收months
        List<String> months = new ArrayList<>();
        //使用Calendar 查询当前日期
        Calendar calendar = Calendar.getInstance();
        //要使用的日期是十二个月之前，计算十二个月之前
        calendar.add(Calendar.MONTH,-12);
        //使用for循环输出一年的月份
        for (int i= 0;i<12;i++){
            //用date接收得到的时间
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy,MM").format(date));

        }
        map.put("months",months);

        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount",memberCount);
        return new  Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    /**
     * 导出excel表
     * @param response
     * @return
     */
    @GetMapping("/exportBusinessReport")
    public Result listReport(HttpServletResponse response) throws Exception {
        //获取数据
        orderService.listHotSermeal(response);
        return null;
    }

    //获取运营数据统计
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        //调用service返回封装对象
        Map<String, Object> data = reportService.getBusinessReportData();
        map = data;
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, data);
    }
}
